home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / Z4ZXEX.C < prev    next >
C/C++ Source or Header  |  1993-08-13  |  5KB  |  202 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    z4z4ex.c
  5. //   Title:    ZIP+4 Engine
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains the expander for the ZIP4 cross reference file.
  25. //    This module should not use any global variables since it must be 
  26. //    re-entrant.
  27. //
  28. //    The code in this module should be written entirely in C. 
  29. //    Do not use any C++ constructs.
  30. //
  31. //    This module is portable to:
  32. //        DOS 3.X+
  33. //        MS Windows 3.X+
  34. //        OS/2 2.X+
  35. //        OS/2 2.0 PM
  36. //        SCO UNIX.
  37. //
  38. //    The following compilers are supported:
  39. //        MSC 6.0A
  40. //        MSC/C++ 7.0
  41. //        Borland C++ 3.1 for DOS
  42. //        Borland C++ 1.0 for OS/2 2.X
  43. //        SCO UNIX cc
  44. //
  45. //----------------------------------------------------------------------------
  46. #include <z4.h>
  47.  
  48.  
  49. //----------------------------------------------------------------------------
  50. //   Description:    Read a compressed record from the output buffer.
  51. //    Parameters: pblk            Decoder data structure
  52. //                        pctyst        ZIP5 record
  53. //       Returns:    TRUE if successful. 
  54. //                        FALSE if no more records found to decode.
  55. //----------------------------------------------------------------------------
  56. BOOL FN_E Z4ZXExpand(PZ4_ZX_BLK pblk, PZ4_ZX pzx)
  57. {
  58.     PBYTE pb;
  59.     SIZET cb;
  60.     ULONG ulDelta;
  61.     USHORT usDelta = 0;
  62.     BOOL fDelta;
  63.     CHAR szZip4[MAX_ZIP4+1];
  64.     USHORT usAddonLo, usAddonHi;
  65.  
  66.     Assert(pblk && pzx);
  67.     memset(pzx, 0, sizeof(pzx));
  68.                                                     // Must be at end of buffer
  69.     if (pblk->cbNext >= pblk->cb)
  70.         return FALSE;
  71.     pb = pblk->pb + pblk->cbNext;
  72.     if (pb[0] == 0)                            // Block not full, but no more records
  73.         {
  74.         pblk->cbNext = pblk->cb;
  75.         return FALSE;
  76.         }
  77.  
  78.     //
  79.     //    Decode record
  80.     //
  81.     ulDelta = (ULONG)(pb[0] & 0x3F);
  82.     fDelta = (pb[0] & 0x40) != 0;
  83.     if (pb[0] & 0x80)
  84.         {
  85.         pb++;
  86.         ulDelta += (((ULONG)pb[0]) << 6);
  87.         pb++;
  88.         ulDelta += (((ULONG)pb[0]) << 14);
  89.         pb++;
  90.         ulDelta += (((ULONG)pb[0]) << 22);
  91.         pb++;
  92.         }
  93.     else
  94.         pb++;
  95.  
  96.     pblk->ulZip4 += ulDelta;
  97.     sprintf(szZip4, "%09lu", pblk->ulZip4);
  98.     strcpy(pblk->zx.szAddonLo, szZip4 + MAX_ZIP5);
  99.     usAddonLo = (USHORT)atoi(pblk->zx.szAddonLo);
  100.     szZip4[MAX_ZIP5] = '\0';
  101.     strcpy(pblk->zx.szZip5, szZip4);
  102.  
  103.     //
  104.     //    Get high range delta
  105.     //
  106.     if (fDelta)
  107.         {
  108.        usDelta = (USHORT)(pb[0] & 0x7F);
  109.        if (pb[0] & 0x80)
  110.            {
  111.            pb++;
  112.            usDelta += (USHORT)(((USHORT)pb[0]) << 7);
  113.            pb++;
  114.            }
  115.        else
  116.            pb++;
  117.        usDelta++;
  118.         }
  119.     usAddonHi = usAddonLo + usDelta;
  120.     sprintf(pblk->zx.szAddonHi, "%04u", usAddonHi);
  121.     //
  122.     //    Get record count
  123.     //
  124.     cb = (SIZET)(pb[0] & 0x7F);
  125.     if (pb[0] & 0x80)
  126.         {
  127.         pb++;
  128.         cb += (((SIZET)pb[0]) << 7);
  129.         pb++;
  130.         }
  131.     else
  132.         pb++;
  133.     cb++;
  134.  
  135.     //
  136.     //    Decode record ids
  137.     //
  138.     if (!RecIdDecode(pblk->zx.arecid, MAX_ZIP4_RECS, pb, cb, &pblk->zx.cRecords, &pblk->recid))
  139.         return FALSE;
  140.     Assert(pblk->zx.cRecords <= MAX_ZIP4_RECS);
  141.     pb += cb;
  142.  
  143.     //
  144.     //    Finish up
  145.     //
  146.     cb = (SIZET)(pb - pblk->pb);
  147.     Assert(cb <= pblk->cb);
  148.     pblk->cbNext = cb;
  149.     *pzx = pblk->zx;                            // Return a copy of the current record
  150.     return TRUE;
  151. }
  152.  
  153.  
  154. //----------------------------------------------------------------------------
  155. //   Description:    Initialize ZIP5 expander
  156. //    Parameters:    pblk            Decoder data structure
  157. //       Returns:    TRUE if successful.
  158. //----------------------------------------------------------------------------
  159. BOOL FN_E Z4ZXExpandInitialize(PZ4_ZX_BLK pblk)
  160. {
  161.     Assert(pblk);
  162.     memset(pblk, 0, sizeof(Z4_ZX_BLK));
  163.     return TRUE;
  164. }
  165.  
  166.  
  167. //----------------------------------------------------------------------------
  168. //   Description:    Reset ZIP4 cross reference expander to decode another 
  169. //                          block of data.
  170. //    Parameters:    pblk            Decoder data structure
  171. //                        pb                Buffer containing compressed data.
  172. //                        cb                Size of buffer.
  173. //       Returns:    TRUE if successful.
  174. //----------------------------------------------------------------------------
  175. BOOL FN_E Z4ZXExpandReset(PZ4_ZX_BLK pblk, PBYTE pb, SIZET cb)
  176. {
  177.     Assert(pblk);
  178.     pblk->pb = pb;                                // Set decoding pointers
  179.     pblk->cb = cb;
  180.     pblk->cbNext = 0;
  181.     pblk->ulZip4 = 0;
  182.     pblk->recid.usOffset = 0;
  183.     pblk->recid.lBlock = 0;
  184.     return TRUE;
  185. }
  186.  
  187.  
  188. //----------------------------------------------------------------------------
  189. //   Description:    Terminate ZIP5 expander.
  190. //    Parameters:    pblk        Decoder data structure
  191. //       Returns:    TRUE if successful.
  192. //----------------------------------------------------------------------------
  193. BOOL FN_E Z4ZXExpandTerminate(PZ4_ZX_BLK pblk)
  194. {
  195.     Assert(pblk);
  196.     memset(pblk, 0, sizeof(Z4_ZX_BLK));
  197.     return TRUE;
  198. }
  199. //----------------------------------------------------------------------------
  200. //------------------------------- End of File --------------------------------
  201. //----------------------------------------------------------------------------
  202.